/*     */ package com.zimbra.cs.filter.jsieve;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.util.ListUtil;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.filter.ZimbraMailAdapter;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.mime.ParsedMessage.CalendarPartInfo;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import java.util.TreeSet;
/*     */ import org.apache.jsieve.Argument;
/*     */ import org.apache.jsieve.Arguments;
/*     */ import org.apache.jsieve.SieveContext;
/*     */ import org.apache.jsieve.exception.SieveException;
/*     */ import org.apache.jsieve.mail.MailAdapter;
/*     */ import org.apache.jsieve.tests.AbstractTest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class InviteTest
/*     */   extends AbstractTest
/*     */ {
/*  40 */   private static final Set<String> ALL_METHODS = new TreeSet(String.CASE_INSENSITIVE_ORDER);
/*  41 */   private static final Set<String> ALL_REQUEST_METHODS = new TreeSet(String.CASE_INSENSITIVE_ORDER);
/*  42 */   private static final Set<String> ALL_REPLY_METHODS = new TreeSet(String.CASE_INSENSITIVE_ORDER);
/*     */   
/*     */ 
/*  45 */   private Set<String> mMethods = new TreeSet(String.CASE_INSENSITIVE_ORDER);
/*     */   
/*     */   static {
/*  48 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.PUBLISH);
/*  49 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.REQUEST);
/*  50 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.REPLY);
/*  51 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.ADD);
/*  52 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.CANCEL);
/*  53 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.REFRESH);
/*  54 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.COUNTER);
/*  55 */     addMethod(ALL_METHODS, ZCalendar.ICalTok.DECLINECOUNTER);
/*  56 */     ALL_METHODS.add("anyrequest");
/*  57 */     ALL_METHODS.add("anyreply");
/*     */     
/*  59 */     addMethod(ALL_REQUEST_METHODS, ZCalendar.ICalTok.PUBLISH);
/*  60 */     addMethod(ALL_REQUEST_METHODS, ZCalendar.ICalTok.REQUEST);
/*  61 */     addMethod(ALL_REPLY_METHODS, ZCalendar.ICalTok.REPLY);
/*  62 */     addMethod(ALL_REQUEST_METHODS, ZCalendar.ICalTok.ADD);
/*  63 */     addMethod(ALL_REQUEST_METHODS, ZCalendar.ICalTok.CANCEL);
/*  64 */     addMethod(ALL_REPLY_METHODS, ZCalendar.ICalTok.REFRESH);
/*  65 */     addMethod(ALL_REPLY_METHODS, ZCalendar.ICalTok.COUNTER);
/*  66 */     addMethod(ALL_REQUEST_METHODS, ZCalendar.ICalTok.DECLINECOUNTER); }
/*     */   
/*  68 */   private static final String ARG_SPEC = ":method [" + StringUtil.join("|", ALL_METHODS) + ", ...]";
/*     */   
/*     */   private static void addMethod(Set<String> set, ZCalendar.ICalTok method)
/*     */   {
/*  72 */     set.add(method.toString().toLowerCase());
/*     */   }
/*     */   
/*     */ 
/*     */   protected void validateArguments(Arguments arguments, SieveContext context)
/*     */     throws SieveException
/*     */   {
/*  79 */     List<Argument> argList = arguments.getArgumentList();
/*  80 */     if (ListUtil.isEmpty(argList))
/*     */     {
/*  82 */       return;
/*     */     }
/*     */     
/*  85 */     if (argList.size() != 2) {
/*  86 */       validationError(argList);
/*     */     }
/*  88 */     Object arg1 = ((Argument)argList.get(0)).getValue();
/*  89 */     Object arg2 = ((Argument)argList.get(1)).getValue();
/*     */     
/*  91 */     if (!(arg1 instanceof String)) {
/*  92 */       validationError(argList);
/*     */     }
/*  94 */     if (!((String)arg1).equals(":method")) {
/*  95 */       validationError(argList);
/*     */     }
/*  97 */     if (!(arg2 instanceof List)) {
/*  98 */       validationError(argList);
/*     */     }
/* 100 */     if (((List)arg2).isEmpty()) {
/* 101 */       validationError(argList);
/*     */     }
/* 103 */     for (String methodArg : (List)arg2) {
/* 104 */       if (!ALL_METHODS.contains(methodArg)) {
/* 105 */         validationError(argList);
/*     */       }
/* 107 */       this.mMethods.add(methodArg);
/*     */     }
/*     */   }
/*     */   
/*     */   private void validationError(List<Argument> argList) throws SieveException
/*     */   {
/* 113 */     throw new SieveException("Invalid arguments to the invite test.  Expected " + ARG_SPEC + ", got " + argList);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context)
/*     */   {
/* 121 */     if (!(mail instanceof ZimbraMailAdapter))
/* 122 */       return false;
/* 123 */     ParsedMessage pm = ((ZimbraMailAdapter)mail).getParsedMessage();
/* 124 */     if (pm == null) {
/* 125 */       return false;
/*     */     }
/* 127 */     ParsedMessage.CalendarPartInfo calPart = pm.getCalendarPartInfo();
/* 128 */     if (calPart != null) {
/* 129 */       if (this.mMethods.isEmpty())
/*     */       {
/* 131 */         return true;
/*     */       }
/* 133 */       if (calPart.method == null)
/*     */       {
/* 135 */         return false;
/*     */       }
/* 137 */       if (this.mMethods.contains(calPart.method.toString()))
/*     */       {
/* 139 */         return true;
/*     */       }
/* 141 */       if ((this.mMethods.contains("anyrequest")) && (isRequest(calPart.method))) {
/* 142 */         return true;
/*     */       }
/* 144 */       if ((this.mMethods.contains("anyreply")) && (isReply(calPart.method))) {
/* 145 */         return true;
/*     */       }
/*     */     }
/* 148 */     return false;
/*     */   }
/*     */   
/*     */   private static boolean isRequest(ZCalendar.ICalTok method) {
/* 152 */     if ((method != null) && (ALL_REQUEST_METHODS.contains(method.toString()))) {
/* 153 */       return true;
/*     */     }
/* 155 */     return false;
/*     */   }
/*     */   
/*     */   private static boolean isReply(ZCalendar.ICalTok method) {
/* 159 */     if ((method != null) && (ALL_REPLY_METHODS.contains(method.toString()))) {
/* 160 */       return true;
/*     */     }
/* 162 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/jsieve/InviteTest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */